<?php
/**
 * Created by JetBrains PhpStorm.
 * User: jun
 * Date: 14-5-8
 * Time: 下午3:54
 * To change this template use File | Settings | File Templates.
 */
require_once 'check.php';
require_once 'common.php';
require_once 'quickDeal.php';
function  quick_business($req){
    try{
        if(!quick_param_check($req)){
            return returnMsg(1,"参数错误",null);
        }
        $identify = $req["identify"];
        $userMsgArray = getUserMsg($identify);
        if($userMsgArray=="error"){
            return returnMsg(1,"用户信息异常",null);
        }
        $userId = $userMsgArray["id"];
        $userNickName =  $userMsgArray["user_nickname"];
        $userPhone = $userMsgArray["user_phone"];
        $date = date("Y-m-d");
        $time = date("H:i:s");
        $merchant_phone =  $req["merchant_phone"];


        if(!selectSameData($userId,$date)){
            return returnMsg(0,"操作成功",null);
        }
        //插入抢单表
        $sql = "insert into t_quick_buying (user_id,merchant_phone,business_date,business_time,user_nickname,user_phone)
        value($userId,$merchant_phone,'$date','$time','$userNickName',$userPhone)";
        $dbDeal = new crud();
        $res = $dbDeal ->dbExec($sql);
        if($res=="error"){
            return returnMsg(1,"系统异常，请稍后重试",null);
        }
        return returnMsg(0,"操作成功",null);

    }catch (Exception $e){
        throw new Exception($e->getMessage());
    }

}

function getUserMsg($arg_identify){
    try{
        $sql = "select id,user_nickname,user_phone from t_user where idenity = '$arg_identify'";
        $dbDeal = new crud();
        $res = $dbDeal->dbSelect($sql);
        if(!check_user_msg($res[0])){
            return "error";
        }
        return $res[0];

    }catch (Exception $e){
        throw new Exception($e->getMessage());
    }
}

function selectSameData($arg_id,$arg_date){
    $sql = "select count(id) as count from t_quick_buying where user_id = $arg_id and business_date = '$arg_date'";
    $dbDeal = new crud();
    $res = $dbDeal->dbSelect($sql);
    if($res[0]["count"]>0){
        return false;
    }
   return true;

}

